﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class SetToImportDatabase : Form
    {
        public SetToImportDatabase()
        {
            InitializeComponent();
        }

        public EventScavengerDAL EventScavengerDAL { get; set; }
        public string SelectedSqlServer { get; set; }
        public string SelectedDatabase { get; set; }

        private void SetToImportDatabase_Load(object sender, EventArgs e)
        {
            if (EventScavengerDAL != null)
                LoadDBImportNames();
            cboServer.Text = SelectedSqlServer;
            cboDatabase.Text = SelectedDatabase;
        }

        private void LoadDBImportNames()
        {
            try
            {
                List<Tuple<string, string>> list = EventScavengerDAL.GetExistingDBImportNames();
                cboServer.Items.Clear();
                cboDatabase.Items.Clear();
                foreach (Tuple<string, string> entry in list)
                {
                    if (entry.Item1.Length > 0 && !cboServer.Items.Contains(entry.Item1))
                        cboServer.Items.Add(entry.Item1);
                    if (entry.Item2.Length > 0 && !cboDatabase.Items.Contains(entry.Item2))
                        cboDatabase.Items.Add(entry.Item2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Import DB names", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckOKEnabled();
        }

        private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckOKEnabled();
        }

        private void CheckOKEnabled()
        {
            cmdOK.Enabled = cboServer.Text.Length > 0 && cboDatabase.Text.Length > 0;
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                EventScavenger.EventScavengerDAL testDal = new EventScavengerDAL();
                testDal.Server = cboServer.Text;
                testDal.Database = cboDatabase.Text;
                testDal.SetConnection();
                SelectedSqlServer = cboServer.Text;
                SelectedDatabase = cboDatabase.Text;
                if (testDal.TestConnection())
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                else
                {
                    System.Windows.Forms.DialogResult result = MessageBox.Show("Connection could not be verified!\r\nAccept anyway?", "Fail", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (result == System.Windows.Forms.DialogResult.Cancel)
                    {
                        return;
                    }
                    else
                        DialogResult = System.Windows.Forms.DialogResult.Cancel;
                }
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cboServer_TextChanged(object sender, EventArgs e)
        {
            CheckOKEnabled();
        }

        private void cboDatabase_TextChanged(object sender, EventArgs e)
        {
            CheckOKEnabled();
        }
    }
}
